/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zhuangfei.ohos_timetableview.slice;

import com.zhuangfei.ohos_timetableview.ResourceTable;
import com.zhuangfei.ohos_timetableview.base.BaseAbilitySlice;
import com.zhuangfei.ohos_timetableview.model.MySubject;
import com.zhuangfei.ohos_timetableview.model.SubjectRepertory;
import com.zhuangfei.ohos_timetableview.spinner.MaterialSpinner;
import com.zhuangfei.timetable.TimetableView;

import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ColorPoolSlice
 *
 * @since 2021-03-29
 */
public class ColorPoolSlice extends BaseAbilitySlice {
    private static final String[] SPIN_MUNE = {"指定三种颜色", "默认颜色池", "扩充颜色池", "指定课程颜色"};
    TimetableView mTimetableView;

    MaterialSpinner moreButton;
    Intent intents;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_color_pool);
        initViews();
        intents = intent;
    }

    private void initViews() {
        moreButton = (MaterialSpinner) findComponentById(ResourceTable.Id_id_more);

        moreButton.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
                selectLayout(position);
                moreButton.setShowFalse();

            }
        });
        moreButton.setItems(SPIN_MUNE);
        moreButton.setOnNothingSelectedListener(new MaterialSpinner.OnNothingSelectedListener() {

            @Override
            public void onNothingSelected(MaterialSpinner spinner) {
                spinner.setShowFalse();
            }
        });

        initTimetableView();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * 初始化课程控件
     */
    private void initTimetableView() {
        mTimetableView = (TimetableView) findComponentById(ResourceTable.Id_id_timetableView);

        List<MySubject> mySubjects = SubjectRepertory.loadDefaultSubjects();
        mTimetableView.source(mySubjects)
                .curWeek(1)
                .curTerm("大三下学期")
                .showView();
    }

    private void selectLayout(int position) {
        switch (position) {
            case 0:
                setColor(new RgbColor(0, 0, 255), new RgbColor(255, 255, 0), new RgbColor(0, 255, 255));
                break;
            case 1:
                resetColor();
                break;
            case 2:
                addColor(new RgbColor(0, 0, 255), new RgbColor(255, 255, 0), new RgbColor(0, 255, 255));
                break;
            case 3:
                forColor();
                break;
            default:
                break;
        }
    }

    /**
     * 设置指定的颜色，默认情况下颜色池中有一些颜色
     * 所以这里需要先清空一下颜色池
     *
     * @param colors
     */
    public void setColor(RgbColor... colors) {
        mTimetableView.colorPool().clear().add(colors);
        mTimetableView.updateView();
    }

    /**
     * 重置颜色池
     */
    public void resetColor() {
        mTimetableView.colorPool().reset();
        mTimetableView.updateView();
    }

    /**
     * 追加颜色
     *
     * @param colors
     */
    public void addColor(RgbColor... colors) {
        mTimetableView.colorPool().add(colors);
        mTimetableView.updateView();
    }

    /**
     * 指定课程的颜色，未指定的课程自动分配
     */
    public void forColor() {
        Map<String, RgbColor> colorMap = new HashMap<>();
        colorMap.put("数字图像处理", new RgbColor(255, 0, 0));
        colorMap.put("算法分析与设计", new RgbColor(0, 0, 255));
        mTimetableView.colorPool().setIgnoreUserlessColor(false).setColorMap(colorMap);
        mTimetableView.updateView();
    }
}
